package abio.XasML.rpc
{
	import flash.net.NetConnection;
	import flash.net.Responder;
	import flash.utils.Dictionary;
	import flash.utils.Proxy;
	import flash.utils.flash_proxy;
	
	import mx.core.mx_internal;
	import mx.messaging.messages.AbstractMessage;
	import mx.rpc.AsyncToken;
	import mx.rpc.events.ResultEvent;
	
	dynamic public class connectionService extends Proxy
	{
		static private const netConnectionDict:Dictionary=new Dictionary();
		private var url:String=null;
		
		public function connectionService(url:String)
		{
			this.url=url;
			if(connectionService.netConnectionDict.hasOwnProperty(url)==false){
				connectionService.netConnectionDict[url]=new NetConnection();
				(connectionService.netConnectionDict[url] as NetConnection).connect(url);
			}
		}
		
		override flash_proxy function callProperty(name:*, ...rest):*
		{
			var tenpFunc:Function=this[name];
			return tenpFunc.apply(null, rest);
		}
		
		override flash_proxy function getProperty(name:*):*
		{
			return function delayedCall(...args):AsyncToken{
				var result:AsyncToken=new AsyncToken(new AbstractMessage());				
				var tempFunc:Function=(connectionService.netConnectionDict[url] as NetConnection).call;
				var tempResponder:Responder=new Responder(function RESULT(data:Object):void{
					result.mx_internal::applyResult(new ResultEvent(typeof(data),false,true,data));
				},function FAULT(info:Object):void{
					throw('connectionService error');
				});
				var tempArgs:Array=[name, tempResponder];
				tempFunc.apply((connectionService.netConnectionDict[url] as NetConnection),tempArgs.concat(args));
				return result;
			};
		}
	}
}